package com.link.base.base.orderpayset.service;

import com.link.base.base.orderpayset.dao.mybatis.mapper.OrderPaySetMapper;
import com.link.base.base.orderpayset.model.OrderPaySet;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author liushoulong
 * @date 2018/4/27
 * @comments 订单参数设置服务
 */
@Service
public class OrderPaySetServiceImpl extends BasicServiceImpl<OrderPaySet> implements OrderPaySetService {

    @Resource
    private OrderPaySetMapper orderPaySetMapper;

    @Override
    public BasicMapper<OrderPaySet> getBasicMapper() {
        return orderPaySetMapper;
    }


    /**
     * @auther HuangLJ
     * @param orderPaySet
     * @return
     * @throws Exception
     * @Comments 通过订单类型获取订单参数信息（一级类型，二级类型）
     */
    @Override
    public OrderPaySet getByOrderType(OrderPaySet orderPaySet) throws Exception {
        //校验数据，如果为传入一级类型和二级类型，则报错
        // 订单一级类型
        String firstType = orderPaySet.getOrderFirstType();
        //订单二级类型
        String secondType = orderPaySet.getOrderSecondType();
        //订单大类
        String ordKind = orderPaySet.getOrderKind();

        if (StringUtils.isEmpty(firstType) || StringUtils.isEmpty(secondType) || StringUtils.isEmpty(ordKind)) {
            throw new ServiceException("ORDER-049");
        }

//      OrderPaySet orderPaySetRet = null;
        List<OrderPaySet> orderPaySetList = this.queryAll(orderPaySet);
        //如果设置条数大于1 那么就出现非法数据错误
        if (!CollectionUtils.isEmpty(orderPaySetList) && orderPaySetList.size() > 1) {
            throw new ServiceException("PUBLIC-032");
        } else if (!CollectionUtils.isEmpty(orderPaySetList) && orderPaySetList.size() == 1) {
            return orderPaySetList.get(0);
        }
//        else {  //新建
//            orderPaySet.setCheckBalancedFlag("N");
//            orderPaySet.setCheckSaleFlag("N");
//            orderPaySet.setCheckStoredFlag("N");
//            orderPaySet.setCheckPolicyFlag("N");  //默认不做操作
//            this.insert(orderPaySet);
//            List<OrderPaySet> orderPaySetList1 = this.queryAll(orderPaySet);
//            orderPaySetRet = orderPaySetList1.get(0);
//        }
        return null;
    }
}
